package com.zzyl.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.util.retry.Retry;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        // 1、myubatisplus方式实现
        /*// 1、查询护理计划主表数据
        NursingPlan nursingPlan = getById(id);
        if(ObjectUtil.isEmpty(nursingPlan)){
            return null;
        }
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);

        // 2、查询护理计划关联的 护理项目子表数据
        List<NursingProjectPlan> list = nursingProjectPlanService.lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id)
                .list();

        if(ObjectUtil.isNotEmpty(list)){
            List<NursingProjectPlanVo> projectPlanVoList = list.stream().map(projectPlan -> {
                NursingProjectPlanVo projectPlanVo = BeanUtil.toBean(projectPlan, NursingProjectPlanVo.class);
                projectPlanVo.setProjectId(String.valueOf(projectPlan.getProjectId()));
                projectPlanVo.setExecuteCycle(String.valueOf(projectPlan.getExecuteCycle()));
                projectPlanVo.setExecuteFrequency(projectPlan.getExecuteFrequency().longValue());
                return projectPlanVo;
            }).collect(Collectors.toList());
             nursingPlanVo.setProjectPlans(projectPlanVoList);
        }

        // 3、返回结果
        return nursingPlanVo;*/

        // 方式二：mybatis原生 一对多查询实现
        return nursingPlanMapper.selectNursingPlanVoById(id);
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        // 1、保存护理计划主表数据
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        this.save(nursingPlan); // mybatisplus 的 save 保存完数据后，主键会自动回填

        // 2、批量保存护理计划项目 子表数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (ObjectUtil.isEmpty(projectPlans)) {
            return 1;
        }
        projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        boolean result = this.nursingProjectPlanService.saveBatch(projectPlans);

        // 3、返回结果
        return result ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        // 1、修改护理计划关联的护理项目子表
        // 1.1、先删除子表数据
        //LambdaQueryWrapper<NursingProjectPlan> eq1 = Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId());
        //LambdaQueryWrapper<NursingProjectPlan> eq2 = new LambdaQueryWrapper<NursingProjectPlan>().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId());
        this.nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));

        // 1.2、判断前端是否有提交子表数据，如果有，则进行批量保存
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(ObjectUtil.isNotEmpty(projectPlans)){
            projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlanDto.getId()));
            this.nursingProjectPlanService.saveBatch(projectPlans);
        }

        // 2、修改护理计划主表数据
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        return this.updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanByIds(Long[] ids) {
        // 1.批量删除护理计划关联的护理项目
        // delete from nursing_project_plan where plan_id in (1,2,3)
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().in(NursingProjectPlan::getPlanId, ids));

        // 2.删除护理计划基本信息
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }
}
